package com.yonyou.pmclouds.nc.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.conf.NCInvokerConfiguration;
import com.yonyou.pmclouds.basecom.conf.NCServiceVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping(value = "/nc")
public class NCLinkQueryController {


    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private NCInvokerConfiguration.Invoker invoker;

    private ObjectMapper mapper = new ObjectMapper();

    private final String serviceName = "PMCLinkUrlServlet";

    private final String servicePath = "/service/PMCLinkUrlServlet";

    private final String serviceImpl = "nc.bs.pmcloud.servlet.sso.PMCLinkUrlQuery4UBL";

    @RequestMapping(value = "/getNccLinkQueryUrl")
    @ResponseBody
    public Object getNccLinkQueryUrl(@RequestBody LinkQueryVO linkQueryVO) throws BusinessException {

        Map resultMap = new HashMap<>();

        if(linkQueryVO == null || StringUtils.isEmpty(linkQueryVO.getBilltype()))
            return resultMap;

        NCServiceVO linkService = new NCServiceVO(serviceName, servicePath, serviceImpl);

        Map<String,Object> nccLinkQueryMap = getNcParam(linkQueryVO);
        if(nccLinkQueryMap == null || nccLinkQueryMap.size() == 0)
            return resultMap;

        NCInvokerConfiguration.Result result = invoker.invokeSync(linkService,nccLinkQueryMap,RuntimeEnvironment.getTenantId(),RuntimeEnvironment.getLoginUserId());

        if (result == null || result.getResult() == null) {
            log.error("nc查询失败, 返回result: {}", result == null ? null : String.valueOf(result.getResult()));
            return resultMap;
        }

        if (!NCInvokerConfiguration.Result.STATUS_OK.equals(result.getStatus())) {
            log.error("nc查询失败, 返回status: {}, result: {}", result.getStatus(), result.getResult());
            return resultMap;
        }

        try {
            resultMap = mapper.readValue(result.getResult(), Map.class);
            if("ok".equals(resultMap.get("resultCode"))){
                return resultMap.get("result");
            }else{
                log.error("nc单据类型查询失败, 返回result: {}", resultMap.get("result"));
            }
        } catch (IOException e) {
            log.error("ncc单据单点路径获取失败", e);
            return resultMap;
        }
        return resultMap;
    }

    public Map<String,Object> getNcParam(LinkQueryVO queryVO){

        Map<String,Object> ncParam = new HashMap<>();
//
//        UserInfo userInfo = userInfoQuery.queryUser(RuntimeEnvironment.getLoginUserId());
//        if(userInfo == null || StringUtils.isEmpty(userInfo.getUserMobile()) ){
//            log.error("未查询到当前人员手机号");
//            return ncParam;
//        }
        ncParam.put("billtype",queryVO.getBilltype());
        return ncParam;
    }

}

@Data
class NCCLinkQueryVO implements Serializable{
    private String userMobile;
    private String clientId;
    private String security;
    private String billType;
}

@Data
class LinkQueryVO implements Serializable {
    private String billtype;
}

@Data
class NCCLinkResultVO implements Serializable {
    private String appcode;
    private String pagecode;
    private String name;
    private String pageurl;
    private String appid;
}